/*
 *
 *    Copyright (c) 2020-2022 Project CHIP Authors
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

/**
 *    @file
 *          Platform-specific configuration overrides for CHIP on
 *          nRF Connect SDK platform.
 */

#pragma once

#ifdef CONFIG_CHIP_CRYPTO_PSA
#include <psa/crypto.h>
#endif

// ==================== General Platform Adaptations ====================

#define CHIP_CONFIG_ABORT() abort()

#define CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE const char *
#define CHIP_CONFIG_PERSISTED_STORAGE_MAX_KEY_LENGTH 2

#define CHIP_CONFIG_LIFETIIME_PERSISTED_COUNTER_KEY "rc"

// ==================== Security Adaptations ====================

#ifdef CONFIG_CHIP_CRYPTO_PSA
#define CHIP_CONFIG_SHA256_CONTEXT_SIZE sizeof(psa_hash_operation_t)
#define CHIP_CONFIG_HKDF_KEY_HANDLE_CONTEXT_SIZE sizeof(psa_key_id_t)
#elif defined(CONFIG_CC3XX_BACKEND)
// Size of the statically allocated context for SHA256 operations in CryptoPAL
// determined empirically.
#define CHIP_CONFIG_SHA256_CONTEXT_SIZE 244
#else
#define CHIP_CONFIG_SHA256_CONTEXT_SIZE 208
#endif

#ifdef CONFIG_CHIP_CRYPTO_PSA
#ifndef CHIP_CONFIG_SHA256_CONTEXT_ALIGN
#define CHIP_CONFIG_SHA256_CONTEXT_ALIGN psa_hash_operation_t
#endif // CHIP_CONFIG_SHA256_CONTEXT_ALIGN
#endif // CONFIG_CHIP_CRYPTO_PSA

#ifndef CHIP_CONFIG_CRYPTO_PSA_KEY_ID_BASE
#define CHIP_CONFIG_CRYPTO_PSA_KEY_ID_BASE 0x30000
#endif // CHIP_CONFIG_CRYPTO_PSA_KEY_ID_BASE

#ifndef CHIP_CONFIG_CRYPTO_PSA_KEY_ID_END
#define CHIP_CONFIG_CRYPTO_PSA_KEY_ID_END 0x3FFFF
#endif // CHIP_CONFIG_CRYPTO_PSA_KEY_ID_END

// ==================== General Configuration Overrides ====================

#ifndef CHIP_CONFIG_MAX_UNSOLICITED_MESSAGE_HANDLERS
#define CHIP_CONFIG_MAX_UNSOLICITED_MESSAGE_HANDLERS 8
#endif // CHIP_CONFIG_MAX_UNSOLICITED_MESSAGE_HANDLERS

#ifndef CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS
#define CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS 8
#endif // CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS

#ifndef CHIP_LOG_FILTERING
#define CHIP_LOG_FILTERING 0
#endif // CHIP_LOG_FILTERING

#ifndef CHIP_CONFIG_BDX_MAX_NUM_TRANSFERS
#define CHIP_CONFIG_BDX_MAX_NUM_TRANSFERS 1
#endif // CHIP_CONFIG_BDX_MAX_NUM_TRANSFERS

#ifdef CONFIG_CHIP_MAX_FABRICS
#define CHIP_CONFIG_MAX_FABRICS CONFIG_CHIP_MAX_FABRICS
#endif // CONFIG_CHIP_MAX_FABRICS

#ifdef CONFIG_CHIP_MAX_ACTIVE_CASE_CLIENTS
#define CHIP_CONFIG_DEVICE_MAX_ACTIVE_CASE_CLIENTS CONFIG_CHIP_MAX_ACTIVE_CASE_CLIENTS
#endif // CONFIG_CHIP_MAX_ACTIVE_CASE_CLIENTS

#ifdef CONFIG_CHIP_MAX_ACTIVE_DEVICES
#define CHIP_CONFIG_DEVICE_MAX_ACTIVE_DEVICES CONFIG_CHIP_MAX_ACTIVE_DEVICES
#endif // CONFIG_CHIP_MAX_ACTIVE_DEVICES

#ifdef CONFIG_CHIP_SUBSCRIPTION_RESUMPTION_MIN_RETRY_INTERVAL
#define CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION_MIN_RETRY_INTERVAL_SECS CONFIG_CHIP_SUBSCRIPTION_RESUMPTION_MIN_RETRY_INTERVAL
#endif // CONFIG_CHIP_SUBSCRIPTION_RESUMPTION_MIN_RETRY_INTERVAL

#ifdef CONFIG_CHIP_SUBSCRIPTION_RESUMPTION_RETRY_MULTIPLIER
#define CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION_WAIT_TIME_MULTIPLIER_SECS CONFIG_CHIP_SUBSCRIPTION_RESUMPTION_RETRY_MULTIPLIER
#endif // CONFIG_CHIP_SUBSCRIPTION_RESUMPTION_RETRY_MULTIPLIER

#ifdef CONFIG_CHIP_LOG_SIZE_OPTIMIZATION
// Disable some of the too detailed log modules to save flash
#define CHIP_CONFIG_LOG_MODULE_ExchangeManager_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_Crypto_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_Crypto_PROGRESS 0
#define CHIP_CONFIG_LOG_MODULE_BDX_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_BDX_PROGRESS 0
#define CHIP_CONFIG_LOG_MODULE_EventLogging_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_EventLogging_PROGRESS 0
#define CHIP_CONFIG_LOG_MODULE_SetupPayload_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_SetupPayload_PROGRESS 0
#define CHIP_CONFIG_LOG_MODULE_CASESessionManager_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_CASESessionManager_PROGRESS 0
#define CHIP_CONFIG_LOG_MODULE_DataManagement_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_FabricProvisioning_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_chipSystemLayer_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_chipSystemLayer_PROGRESS 0
#define CHIP_CONFIG_LOG_MODULE_Zcl_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_SecureChannel_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_Ble_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_AppServer_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_Support_DETAIL 0
#define CHIP_CONFIG_LOG_MODULE_Support_PROGRESS 0
#endif

// Enable ACL extensions for nRF Connect platform
#ifndef CHIP_CONFIG_ENABLE_ACL_EXTENSIONS
#define CHIP_CONFIG_ENABLE_ACL_EXTENSIONS 1
#endif // CHIP_CONFIG_ENABLE_ACL_EXTENSIONS

#ifndef CHIP_CONFIG_MRP_LOCAL_ACTIVE_RETRY_INTERVAL
#ifndef CONFIG_NET_L2_OPENTHREAD
#define CHIP_CONFIG_MRP_LOCAL_ACTIVE_RETRY_INTERVAL (1000_ms32)
#endif // CONFIG_NET_L2_OPENTHREAD
#endif // CHIP_CONFIG_MRP_LOCAL_ACTIVE_RETRY_INTERVAL

#ifndef CHIP_CONFIG_MRP_LOCAL_IDLE_RETRY_INTERVAL
#ifndef CONFIG_NET_L2_OPENTHREAD
#define CHIP_CONFIG_MRP_LOCAL_IDLE_RETRY_INTERVAL (1000_ms32)
#endif // CONFIG_NET_L2_OPENTHREAD
#endif // CHIP_CONFIG_MRP_LOCAL_IDLE_RETRY_INTERVAL

#ifndef CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC
#ifdef CONFIG_CHIP_ICD_IDLE_MODE_DURATION
#define CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC CONFIG_CHIP_ICD_IDLE_MODE_DURATION
#endif // CONFIG_CHIP_ICD_IDLE_MODE_DURATION
#endif // CHIP_CONFIG_ICD_IDLE_MODE_DURATION_SEC

#ifndef CHIP_CONFIG_ICD_ACTIVE_MODE_DURATION_MS
#ifdef CONFIG_CHIP_ICD_ACTIVE_MODE_DURATION
#define CHIP_CONFIG_ICD_ACTIVE_MODE_DURATION_MS CONFIG_CHIP_ICD_ACTIVE_MODE_DURATION
#endif // CONFIG_CHIP_ICD_ACTIVE_MODE_DURATION
#endif // CHIP_CONFIG_ICD_ACTIVE_MODE_DURATION_MS

#ifndef CHIP_CONFIG_ICD_ACTIVE_MODE_THRESHOLD_MS
#ifdef CONFIG_CHIP_ICD_ACTIVE_MODE_THRESHOLD
#define CHIP_CONFIG_ICD_ACTIVE_MODE_THRESHOLD_MS CONFIG_CHIP_ICD_ACTIVE_MODE_THRESHOLD
#endif // CONFIG_CHIP_ICD_ACTIVE_MODE_THRESHOLD
#endif // CHIP_CONFIG_ICD_ACTIVE_MODE_THRESHOLD_MS

#ifndef CHIP_CONFIG_ICD_CLIENTS_SUPPORTED_PER_FABRIC
#ifdef CONFIG_CHIP_ICD_CLIENTS_PER_FABRIC
#define CHIP_CONFIG_ICD_CLIENTS_SUPPORTED_PER_FABRIC CONFIG_CHIP_ICD_CLIENTS_PER_FABRIC
#endif // CONFIG_CHIP_ICD_CLIENTS_PER_FABRIC
#endif // CHIP_CONFIG_ICD_CLIENTS_SUPPORTED_PER_FABRIC

#ifndef CHIP_CONFIG_CRYPTO_PSA_ICD_MAX_CLIENTS
#define CHIP_CONFIG_CRYPTO_PSA_ICD_MAX_CLIENTS 256
#endif

#ifndef CHIP_CONFIG_ENABLE_BDX_LOG_TRANSFER
#ifdef CONFIG_CHIP_ENABLE_BDX_LOG_TRANSFER
#define CHIP_CONFIG_ENABLE_BDX_LOG_TRANSFER CONFIG_CHIP_ENABLE_BDX_LOG_TRANSFER
#endif // CONFIG_CHIP_ENABLE_BDX_LOG_TRANSFER
#endif // CHIP_CONFIG_ENABLE_BDX_LOG_TRANSFER

#ifdef CONFIG_CHIP_LOG_VERIFY_OR_DIE
#define CHIP_CONFIG_VERBOSE_VERIFY_OR_DIE 1
#ifndef CHIP_CONFIG_VERBOSE_VERIFY_OR_DIE_NO_COND
#define CHIP_CONFIG_VERBOSE_VERIFY_OR_DIE_NO_COND 1
#endif // CHIP_CONFIG_VERBOSE_VERIFY_OR_DIE_NO_COND
#endif // CONFIG_CHIP_LOG_VERIFY_OR_DIE
